package com.yanftch.json2ets.json2ets.views;



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class SetGetChooseDialog extends JDialog {
    private JButton btnCancel;
    private JButton btnOk;
    private JLabel lblMessage;  // 新增的JLabel，用于显示文字
    private OnButtonClickListener mOnButtonClickListener;

    public SetGetChooseDialog() {
        // 设置Dialog的基本属性
        setTitle("Set/Get Choose Dialog");
        setModal(true);
        setSize(300, 200);
        setLocationRelativeTo(null);

        // 设置布局
        setLayout(new BorderLayout());

        // 创建用于显示信息的标签
        lblMessage = new JLabel("是否要生成Class中的 set()/get() 方法？", JLabel.CENTER);
        add(lblMessage, BorderLayout.CENTER);  // 将标签添加到中央区域

        // 创建内容面板（按钮区域）
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());

        // 创建按钮
        btnCancel = new JButton("否");
        btnOk = new JButton("是");

        // 添加按钮到面板
        buttonPanel.add(btnOk);
        buttonPanel.add(btnCancel);

        // 将按钮面板添加到Dialog的底部
        add(buttonPanel, BorderLayout.SOUTH);

        // 设置窗口关闭行为
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 监听按钮点击事件
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        // 按下ESC键时触发取消
        registerEscapeKey();

        // 设置OK按钮为默认按钮
        getRootPane().setDefaultButton(btnCancel);
    }

    private void registerEscapeKey() {
        // 注册ESC键触发取消事件
        getRootPane().registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        if (mOnButtonClickListener != null) {
            mOnButtonClickListener.isConfirm(true);
        }
        dispose();
    }

    private void onCancel() {
        if (mOnButtonClickListener != null) {
            mOnButtonClickListener.isConfirm(false);
        }
        dispose();
    }

    public void setOnButtonClickListener(OnButtonClickListener l) {
        mOnButtonClickListener = l;
    }

    public interface OnButtonClickListener {
        void isConfirm(boolean isConfirm);
    }

    public static void main(String[] args) {
        // 创建并显示对话框
        SetGetChooseDialog dialog = new SetGetChooseDialog();
        dialog.setVisible(true);
        System.exit(0);
    }
}
